package com.gem.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.back.entity.AoaScheduleList;
import com.gem.back.entity.AoaUser;
import com.gem.back.mapper.AoaScheduleListMapper;
import com.gem.back.service.IAoaScheduleListService;
import com.gem.back.service.IAoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Service
public class AoaScheduleListServiceImpl extends ServiceImpl<AoaScheduleListMapper, AoaScheduleList> implements IAoaScheduleListService {
    @Autowired
    private AoaScheduleListMapper aoaScheduleListMapper;
    @Autowired
    private IAoaUserService aoaUserService;
    @Override
    public List<AoaScheduleList> findScheduleList(Integer currentPage, Integer pageCount, String title, Integer id) {
        if (title == null){
            title = "";
        }
        return aoaScheduleListMapper.findScheduleList((currentPage - 1)*pageCount,pageCount,title,id);
    }

    @Override
    public Integer getTotalPage(Integer pageCount, String title, Integer id) {
        if (title == null){
            title = "";
        }
        Integer total = aoaScheduleListMapper.getTotal(title,id);
        return (total - 1) / pageCount + 1;
    }

    @Override
    public void saveSchedule(AoaScheduleList scheduleList, HttpServletRequest request) {
        String remind = request.getParameter("isremind");
        if (remind != null){
            scheduleList.setIsRemind(1);
        }else {
            scheduleList.setIsRemind(0);
        }
        if (scheduleList.getRcId() != null){
            aoaScheduleListMapper.updateById(scheduleList);
        }else {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
            AoaUser aoaUser = aoaUserService.findUserByName(userDetails.getUsername());
            scheduleList.setUserId(aoaUser.getUserId());
            aoaScheduleListMapper.insert(scheduleList);
        }
    }
}
